﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;

namespace TrinityEngine
{
    public static class Input
    {
        private static KeyboardState _lastFrameKeyBoardState = new();

        private static MouseState _lastFrameMouseState = new();

        public static Vector2 mousePosition
        {
            get;
            /*get { return Mouse.GetState().Position.ToVector2(); }*/
        }

        public static bool GetMouseButtonDown(int button)
        {
            switch (button)
            {
                case 0:
                    return _lastFrameMouseState.LeftButton != ButtonState.Pressed && Mouse.GetState().LeftButton == ButtonState.Pressed;
                case 1:
                    return _lastFrameMouseState.RightButton != ButtonState.Pressed && Mouse.GetState().RightButton == ButtonState.Pressed;
                case 2:
                    return _lastFrameMouseState.MiddleButton != ButtonState.Pressed && Mouse.GetState().MiddleButton == ButtonState.Pressed;
                default:
                    Console.WriteLine("Undefined button is illegal!");
                    return false;
            }
        }

        public static bool GetMouseButtonUp(int button)
        {
            switch (button)
            {
                case 0:
                    return _lastFrameMouseState.LeftButton != ButtonState.Released && Mouse.GetState().LeftButton == ButtonState.Released;
                case 1:
                    return _lastFrameMouseState.RightButton != ButtonState.Released && Mouse.GetState().RightButton == ButtonState.Released;
                case 2:
                    return _lastFrameMouseState.MiddleButton != ButtonState.Released && Mouse.GetState().MiddleButton == ButtonState.Released;
                default:
                    Console.WriteLine("Undefined button is illegal!");
                    return false;
            }
        }

        public static bool GetMouseButton(int button)
        {
            switch (button)
            {
                case 0:
                    return Mouse.GetState().LeftButton == ButtonState.Pressed;
                case 1:
                    return Mouse.GetState().RightButton == ButtonState.Pressed;
                case 2:
                    return Mouse.GetState().MiddleButton == ButtonState.Pressed;
                default:
                    Console.WriteLine("Undefined button is illegal!");
                    return false;
            }
        }

        public static bool GetKeyDown(Keys key)
        {
            return _lastFrameKeyBoardState[key] != KeyState.Down && Keyboard.GetState()[key] == KeyState.Down;
        }

        public static bool GetKeyUp(Keys key)
        {
            return _lastFrameKeyBoardState[key] != KeyState.Up && Keyboard.GetState()[key] == KeyState.Up;
        }

        public static bool GetKey(Keys key)
        {
            return Keyboard.GetState().IsKeyDown(key);
        }

        public static int GetAxis(Keys negativeKey, Keys positiveKey)
        {
            int value = 0;

            if (GetKey(negativeKey))
            {
                value -= 1;
            }

            if (GetKey(positiveKey))
            {
                value += 1;
            }

            return value;
        }

        public static Vector2 GetVector(Keys upKey, Keys downKey, Keys leftKey, Keys right)
        {
            return new Vector2(GetAxis(leftKey, right), GetAxis(downKey, upKey));
        }

        public static void LateUpdate()
        {
            _lastFrameKeyBoardState = Keyboard.GetState();
            _lastFrameMouseState = Mouse.GetState();
        }
    }
}
